/**
 * 日结招聘状态字典
 * 包含招聘状态及对应的操作按钮
 */

// 招聘状态枚举
export const RECRUITMENT_STATUS = {
  RECRUITING: 1,     // 招聘中
  STOPPED: 2,        // 停止招聘
  CANCELLED: 3,      // 已取消
  COMPLETED: 4,      // 已完成
} as const;

// 用户角色枚举
export const USER_ROLE = {
  PUBLISHER: 'publisher', // 发布方
  APPLICANT: 'applicant', // 申请者
} as const;

// 操作按钮类型枚举
export const ACTION_TYPE = {
  // 发布方操作
  VIEW_APPLICANTS: 'view_applicants',   // 查看申请者
  EDIT_RECRUITMENT: 'edit_recruitment', // 编辑招聘
  END_RECRUITMENT: 'end_recruitment',   // 结束招聘
  DELETE_RECRUITMENT: 'delete_recruitment', // 删除招聘
  
  // 申请者操作
  APPLY_RECRUITMENT: 'apply_recruitment', // 申请职位
  CANCEL_APPLICATION: 'cancel_application', // 取消申请
  VIEW_DETAILS: 'view_details', // 查看详情
} as const;

// 类型定义
export type RecruitmentStatus = typeof RECRUITMENT_STATUS[keyof typeof RECRUITMENT_STATUS];
export type UserRole = typeof USER_ROLE[keyof typeof USER_ROLE];
export type ActionType = typeof ACTION_TYPE[keyof typeof ACTION_TYPE];

// 状态配置接口
interface StatusConfig {
  label: string;
  color: string;
  bgColor: string;
  borderColor: string;
  publisherActions?: ActionType[];
  applicantActions?: ActionType[];
  actions?: ActionType[];
}

// 操作按钮配置接口
interface ActionConfig {
  label: string;
  type: 'primary' | 'danger' | 'warning' | 'info' | 'default' | 'success';
  size: 'small' | 'normal' | 'large';
  icon: string;
  disabled?: boolean;
}

// 状态配置对象
export const STATUS_CONFIG: Record<number, StatusConfig> = {
  // 招聘状态配置
  [RECRUITMENT_STATUS.RECRUITING]: {
    label: '招聘中',
    color: '#52c41a',
    bgColor: '#f6ffed',
    borderColor: '#b7eb8f',
    publisherActions: [
      ACTION_TYPE.VIEW_APPLICANTS,
      ACTION_TYPE.EDIT_RECRUITMENT,
      ACTION_TYPE.END_RECRUITMENT,
    ],
    applicantActions: [
      ACTION_TYPE.APPLY_RECRUITMENT,
      ACTION_TYPE.VIEW_DETAILS,
    ],
  },
  [RECRUITMENT_STATUS.STOPPED]: {
    label: '停止招聘',
    color: '#faad14',
    bgColor: '#fffbe6',
    borderColor: '#ffe58f',
    publisherActions: [
      ACTION_TYPE.VIEW_APPLICANTS,
      ACTION_TYPE.DELETE_RECRUITMENT,
    ],
    applicantActions: [
      ACTION_TYPE.VIEW_DETAILS,
    ],
  },
  [RECRUITMENT_STATUS.CANCELLED]: {
    label: '已取消',
    color: '#999999',
    bgColor: '#f5f5f5',
    borderColor: '#d9d9d9',
    publisherActions: [
      ACTION_TYPE.DELETE_RECRUITMENT,
    ],
    applicantActions: [
      ACTION_TYPE.VIEW_DETAILS,
    ],
  },
  [RECRUITMENT_STATUS.COMPLETED]: {
    label: '已完成',
    color: '#722ed1',
    bgColor: '#f9f0ff',
    borderColor: '#d3adf7',
    publisherActions: [
      ACTION_TYPE.VIEW_APPLICANTS,
    ],
    applicantActions: [
      ACTION_TYPE.VIEW_DETAILS,
    ],
  },
};

// 操作按钮配置
export const ACTION_CONFIG: Record<ActionType, ActionConfig> = {
  [ACTION_TYPE.VIEW_APPLICANTS]: {
    label: '查看申请者',
    type: 'info',
    size: 'small',
    icon: 'people',
  },
  [ACTION_TYPE.EDIT_RECRUITMENT]: {
    label: '编辑',
    type: 'warning',
    size: 'small',
    icon: 'edit',
  },
  [ACTION_TYPE.END_RECRUITMENT]: {
    label: '结束招聘',
    type: 'danger',
    size: 'small',
    icon: 'stop',
  },
  [ACTION_TYPE.DELETE_RECRUITMENT]: {
    label: '删除',
    type: 'danger',
    size: 'small',
    icon: 'delete',
  },
  [ACTION_TYPE.APPLY_RECRUITMENT]: {
    label: '立即报名',
    type: 'primary',
    size: 'small',
    icon: 'add',
  },
  [ACTION_TYPE.CANCEL_APPLICATION]: {
    label: '取消申请',
    type: 'default',
    size: 'small',
    icon: 'close',
  },
  [ACTION_TYPE.VIEW_DETAILS]: {
    label: '查看详情',
    type: 'info',
    size: 'small',
    icon: 'eye',
  },
};

/**
 * 获取状态配置
 * @param status - 状态值
 * @param statusType - 状态类型 ('recruitment')
 * @returns 状态配置对象
 */
export const getStatusConfig = (status: number, statusType: 'recruitment' = 'recruitment'): StatusConfig | null => {
  return STATUS_CONFIG[status] || null;
};

/**
 * 获取用户在当前状态下的可用操作
 * @param status - 招聘状态
 * @param userRole - 用户角色 ('publisher' | 'applicant')
 * @returns 可用操作列表
 */
export const getAvailableActions = (status: number, userRole: UserRole): ActionType[] => {
  const config = STATUS_CONFIG[status];
  if (!config) return [];
  
  if (userRole === USER_ROLE.PUBLISHER) {
    return config.publisherActions || [];
  } else if (userRole === USER_ROLE.APPLICANT) {
    return config.applicantActions || [];
  }
  
  return [];
};

/**
 * 获取操作按钮配置
 * @param actionType - 操作类型
 * @returns 按钮配置对象
 */
export const getActionConfig = (actionType: ActionType): ActionConfig => {
  return ACTION_CONFIG[actionType] || {} as ActionConfig;
};
